#include "stdafx.h"
#include "wf_drawellipseaxis.h"
#include "pf_snapper.h"

/**
 * Constructor.
 *
 * @param isArc true if this action will produce an ellipse arc. 
 *              false if it will produce a full ellipse.
 */
WF_DrawEllipseAxis::WF_DrawEllipseAxis(
    PF_Container& container,
    PF_GraphicView& graphicView,
    bool isArc)
        :PF_PreviewActionInterface("Draw ellipse with axis",
                           container, graphicView) {

    this->isArc = isArc;
    center = PF_Vector(false);
    major = PF_Vector(false);
    ratio = 0.5;
    angle1 = 0.0;
    angle2 = 2*M_PI;
}



WF_DrawEllipseAxis::~WF_DrawEllipseAxis() {}


QAction* WF_DrawEllipseAxis::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {
    QAction* action;

    if (type==LX_Define::ActionDrawEllipseArcAxis) {
		// (tr("Ellipse Arc with Axis")
        action = new QAction(tr("&Ellipse Arc (Axis)"), NULL);
		action->setIcon(QIcon(":/extui/ellipsearcsaxes.png"));
        //action->zetStatusTip(tr("Draw Ellipse Arcs"));
    } else {
		// tr("Ellipse with Axis")
        action = new QAction(tr("&Ellipse (Axis)"), NULL);
		action->setIcon(QIcon(":/extui/ellipsesaxes.png"));
        //action->zetStatusTip(tr("Draw Ellipses"));
    }
    return action;
}

void WF_DrawEllipseAxis::init(int status) {
    PF_PreviewActionInterface::init(status);

    if (status==SetCenter) {
        center = PF_Vector(false);
    }
    if (status<=SetMajor) {
        major = PF_Vector(false);
    }
    if (status<=SetMinor) {
        ratio = 0.5;
    }
    if (status<=SetAngle1) {
        angle1 = 0.0;
    }
    if (status<=SetAngle2) {
        angle2 = 2*M_PI;
    }
}



void WF_DrawEllipseAxis::trigger() {
    PF_PreviewActionInterface::trigger();

    PF_EllipseAttribute ellipseData(center, major,
                               ratio,
                               angle1, angle2,
                               false);
    PF_Ellipse* ellipse = new PF_Ellipse(container, ellipseData);
    ellipse->setLayerToActive();
    ellipse->setPenToActive();

    container->addEntity(ellipse);

    // upd. undo list:
    if (document!=NULL) {
        document->startUndoCycle();
        document->addUndoable(ellipse);
        document->endUndoCycle();
    }

    PF_Vector rz = graphicView->getRelativeZero();
	graphicView->redraw(LX_Define::RedrawDrawing);
    graphicView->moveRelativeZero(rz);
    drawSnapper();

    setStatus(SetCenter);

    PF_DEBUG->print("PF_ActionDrawEllipseAxis::trigger():"
                    " entity added: %d", ellipse->getId());
}



void WF_DrawEllipseAxis::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawEllipseAxis::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetCenter:
        break;

    case SetMajor:
        if (center.valid) {
            deletePreview();
            PF_EllipseAttribute ed(center, mouse-center,
                              0.5,
                              0.0, 2*M_PI,
                              false);
            preview->addEntity(new PF_Ellipse(preview, ed));
            drawPreview();
        }
        break;

    case SetMinor:
        if (center.valid && major.valid) {
            deletePreview();
            PF_Line line(NULL, PF_LineAttribute(center-major, center+major));
            double d = line.getDistanceToPoint(mouse);
            ratio = d/(line.getLength()/2);
            PF_EllipseAttribute ed(center, major,
                              ratio,
                              0.0, 2*M_PI,
                              false);
            preview->addEntity(new PF_Ellipse(preview, ed));
            drawPreview();
        }
        break;

    case SetAngle1:
        if (center.valid && major.valid) {
            deletePreview();
			
            //angle1 = center.angleTo(mouse);

			PF_Vector m = mouse;
			m.rotate(center, -major.angle());
			PF_Vector v = m-center;
			v.scale(PF_Vector(1.0, 1.0/ratio));
			angle1 = v.angle(); // + major.angle();

            preview->addEntity(new PF_Line(preview, PF_LineAttribute(center, mouse)));
			
            PF_EllipseAttribute ed(center, major,
                              ratio,
                              angle1, angle1+1.0,
                              false);
            preview->addEntity(new PF_Ellipse(preview, ed));
            drawPreview();
        }
        break;

    case SetAngle2:
        if (center.valid && major.valid) {
            deletePreview();
            //angle2 = center.angleTo(mouse);
			
			PF_Vector m = mouse;
			m.rotate(center, -major.angle());
			PF_Vector v = m-center;
			v.scale(PF_Vector(1.0, 1.0/ratio));
			angle2 = v.angle(); // + major.angle();

            preview->addEntity(new PF_Line(preview, PF_LineAttribute(center, mouse)));
			
            PF_EllipseAttribute ed(
                center, major,
                ratio,
                angle1, angle2,
                false);
            preview->addEntity(new PF_Ellipse(preview, ed));
            drawPreview();
        }

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionDrawEllipseAxis::mouseMoveEvent end");
}



void WF_DrawEllipseAxis::mouseReleaseEvent(PF_MouseEvent* e) {
    // Proceed to next status
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    }

    // Return to last status:
    else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}


void WF_DrawEllipseAxis::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }
    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetCenter:
        center = mouse;
        graphicView->moveRelativeZero(mouse);
        setStatus(SetMajor);
        break;

    case SetMajor:
        major = mouse-center;
        setStatus(SetMinor);
        break;

    case SetMinor: {
            PF_Line line(NULL, PF_LineAttribute(center-major, center+major));
            double d = line.getDistanceToPoint(mouse);
            ratio = d/(line.getLength()/2);
            if (!isArc) {
                trigger();
                setStatus(SetCenter);
            } else {
                setStatus(SetAngle1);
            }
        }
        break;

    case SetAngle1: {
        //angle1 = center.angleTo(mouse);
		PF_Vector m = mouse;
		m.rotate(center, -major.angle());
		PF_Vector v = m-center;
		v.scale(PF_Vector(1.0, 1.0/ratio));
		angle1 = v.angle();
        setStatus(SetAngle2);
		} break;

    case SetAngle2: {
        //angle2 = center.angleTo(mouse);
		PF_Vector m = mouse;
		m.rotate(center, -major.angle());
		PF_Vector v = m-center;
		v.scale(PF_Vector(1.0, 1.0/ratio));
		angle2 = v.angle();
        trigger();
        } break;

    default:
        break;
    }
}



void WF_DrawEllipseAxis::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetMinor: {
            bool ok;
            double m = PF_Math::eval(c, &ok);
            if (ok==true) {
                ratio = m / major.magnitude();
                if (!isArc) {
                    trigger();
                } else {
                    setStatus(SetAngle1);
                }
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    case SetAngle1: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                angle1 = PF_Math::deg2rad(a);
                setStatus(SetAngle2);
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    case SetAngle2: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                angle2 = PF_Math::deg2rad(a);
                trigger();
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawEllipseAxis::getAvailableCommands() {
    PF_StringList cmd;
    return cmd;
}



void WF_DrawEllipseAxis::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetCenter:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
                                                tr("Cancel"));
            break;

        case SetMajor:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
                                                tr("Back"));
            break;

        case SetMinor:
            PF_DIALOGFACTORY->updateMouseWidget(
                tr("Specify endpoint or length of minor axis:"),
                tr("Back"));
            break;

        case SetAngle1:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
                                                tr("Back"));
            break;

        case SetAngle2:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
                                                tr("Back"));
            break;

        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawEllipseAxis::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawEllipseAxis::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarEllipses);
        }
    }
}

// EOF
